﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.IO;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SqlClient;
using System.Web.Configuration;
using System.Security.Cryptography;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.ComponentModel;

namespace HospitalSystem.Core
{
    public class RegisterLog
    {
        string strConnString = ConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;

        public Boolean PIDChecker(String PID)
        {
            if (PID.Trim().Length != 13)
                return false;
            int y = 0;
            for (int i = 0; i < PID.Length - 1; i++)
                y += int.Parse(PID[i].ToString()) * (13 - i);
            int x = 11 - (y % 11);
            return PID[12].ToString() == x.ToString();
        }

        public bool ValidateNumber(string input)
        {
            foreach (char c in input)
            {
                if (!Char.IsNumber(c))
                {
                    return false;
                }

            }

            return true;
        }




        public void insertregdata(String CID, String LastName, String FirstName, RadioButtonList Gender, RadioButtonList BloodType, String birthday, String Nation, String Race, String Religion, String Foreigner, String LiveHomeStatus, String EducationStatus, String LiveStreetAddress, String LiveStreetName, String Livetumbon, String LiveCity, String LiveProvince, String LivePostcode, String CensusStreetAddress, String CensusStreetName, String Censustumbon, String CensusCity, String CensusProvince, String CensusPostcode, String HomePhone, String Mobile, String RecordedStaffID, String LiveMoveInDate2, RadioButtonList LiveMoveOutReason, RadioButtonList CensusMoveOutReason, String LiveMoveOutDate2, String CensusMoveInDate2, String CensusMoveOutDate2, DropDownList Titles, DropDownList DisabilityType, String CensusHouseMoo, String CensusHouseName, String CensusHousesoy, String LiveHouseMoo, String LiveHouseName, String LiveHousesoy)
        {
            string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
            SqlConnection conn2 = new SqlConnection(constr);
            SqlCommand CmdSql2 = new SqlCommand("INSERT INTO [Person] (CID,FirstName,LastName) VALUES (@CID,@FirstName,@LastName)", conn2);
            conn2.Open();
            CmdSql2.Parameters.AddWithValue("@CID", CID);
            CmdSql2.Parameters.AddWithValue("@LastName", LastName);
            CmdSql2.Parameters.AddWithValue("@FirstName", FirstName);
            CmdSql2.ExecuteNonQuery();
            conn2.Close();
            //Session["LastName"] = LastName;
            //Session["FirstName"] = FirstName;
            //Session["CID"] = CID;
            SqlCommand CmdSql = new SqlCommand("UPDATE [Person] SET Title=@Title,Gender=@Gender,BloodType=@BloodType,DOB=@DOB,Nation=@Nation,Race=@Race,Religion=@Religion,Foreigner=@Foreigner,LiveHomeStatus=@LiveHomeStatus,EducationStatus=@EducationStatus,LiveStreetAddress=@LiveStreetAddress,LiveStreetName=@LiveStreetName,Livetumbon=@Livetumbon,LiveCity=@LiveCity,LiveProvince=@LiveProvince,LivePostcode=@LivePostcode,CensusStreetAddress=@CensusStreetAddress,CensusStreetName=@CensusStreetName,Censustumbon=@Censustumbon,CensusCity=@CensusCity,CensusProvince=@CensusProvince,CensusPostcode=@CensusPostcode,HomePhone=@HomePhone,Mobile=@Mobile,DateTimeUpdate=@DateTimeUpdate,LiveMoveInDate=@LiveMoveInDate,LiveMoveOutDate=@LiveMoveOutDate,CensusMoveInDate=@CensusMoveInDate,CensusMoveOutDate=@CensusMoveOutDate,LiveMoveOutReason=@LiveMoveOutReason,CensusMoveOutReason=@CensusMoveOutReason,RecordedStaffID=@RecordedStaffID,DisabilityType=@DisabilityType,CensusHouseMoo=@CensusHouseMoo,CensusHouseName=@CensusHouseName,CensusHousesoy=@CensusHousesoy,LiveHouseMoo=@LiveHouseMoo,LiveHouseName=@LiveHouseName,LiveHousesoy=@LiveHousesoy WHERE CID=@CID", conn2);
            conn2.Open();

            CmdSql.Parameters.AddWithValue("@Title", Titles.SelectedValue);
            CmdSql.Parameters.AddWithValue("@Gender", Gender.SelectedValue);
            CmdSql.Parameters.AddWithValue("@BloodType", BloodType.SelectedValue);
            CmdSql.Parameters.AddWithValue("@DOB", birthday);
            CmdSql.Parameters.AddWithValue("@CID", CID);
            CmdSql.Parameters.AddWithValue("@Nation", Nation);
            CmdSql.Parameters.AddWithValue("@Race", Race);
            CmdSql.Parameters.AddWithValue("@Religion", Religion);
            CmdSql.Parameters.AddWithValue("@Foreigner", Foreigner);
            CmdSql.Parameters.AddWithValue("@LiveHomeStatus", LiveHomeStatus);
            CmdSql.Parameters.AddWithValue("@EducationStatus", EducationStatus);
            CmdSql.Parameters.AddWithValue("@LiveStreetAddress", LiveStreetAddress);
            CmdSql.Parameters.AddWithValue("@LiveStreetName", LiveStreetName);
            CmdSql.Parameters.AddWithValue("@Livetumbon", Livetumbon);
            CmdSql.Parameters.AddWithValue("@LiveCity", LiveCity);
            CmdSql.Parameters.AddWithValue("@LiveProvince", LiveProvince);
            CmdSql.Parameters.AddWithValue("@LivePostcode", LivePostcode);
            CmdSql.Parameters.AddWithValue("@CensusStreetAddress", CensusStreetAddress);
            CmdSql.Parameters.AddWithValue("@CensusStreetName", CensusStreetName);
            CmdSql.Parameters.AddWithValue("@Censustumbon", Censustumbon);
            CmdSql.Parameters.AddWithValue("@CensusCity", CensusCity);
            CmdSql.Parameters.AddWithValue("@CensusProvince", CensusProvince);
            CmdSql.Parameters.AddWithValue("@CensusPostcode", CensusPostcode);
            CmdSql.Parameters.AddWithValue("@HomePhone", HomePhone);
            CmdSql.Parameters.AddWithValue("@Mobile", Mobile);
            CmdSql.Parameters.AddWithValue("@RecordedStaffID", RecordedStaffID);
            CmdSql.Parameters.AddWithValue("@DateTimeUpdate", DateTime.Now);
            CmdSql.Parameters.AddWithValue("@LiveMoveInDate", LiveMoveInDate2);
            CmdSql.Parameters.AddWithValue("@LiveMoveOutReason", LiveMoveOutReason.SelectedValue);
            CmdSql.Parameters.AddWithValue("@CensusMoveOutReason", CensusMoveOutReason.SelectedValue);
            CmdSql.Parameters.AddWithValue("@LiveMoveOutDate", LiveMoveOutDate2);
            CmdSql.Parameters.AddWithValue("@CensusMoveOutDate", CensusMoveInDate2);
            CmdSql.Parameters.AddWithValue("@CensusMoveInDate", CensusMoveOutDate2);
            CmdSql.Parameters.AddWithValue("@CensusHouseMoo", CensusHouseMoo);
            CmdSql.Parameters.AddWithValue("@CensusHouseName", CensusHouseName);
            CmdSql.Parameters.AddWithValue("@CensusHousesoy", CensusHousesoy);
            CmdSql.Parameters.AddWithValue("@LiveHouseMoo", LiveHouseMoo);
            CmdSql.Parameters.AddWithValue("@LiveHouseName", LiveHouseName);
            CmdSql.Parameters.AddWithValue("@LiveHousesoy", LiveHousesoy);
            CmdSql.Parameters.AddWithValue("@DisabilityType", DisabilityType.SelectedValue);
            CmdSql.ExecuteNonQuery();
            conn2.Close();
            //MessageBox("ลงทะเบียนเรียบร้อยครับ");
            //ImageUpload_f();CensusHouseMoo,CensusHouseName,CensusHousesoy,LiveHouseMoo,LiveHouseName,LiveHousesoy
            // Response.Redirect("~/regfamily.aspx");
        }



        public void BindDatareg_educationnfo(GridView GridView1, Label cid_label)
        {
            string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
            SqlConnection scon = new SqlConnection(constr);


            //string svccode1 = svccode;
            scon.Open();
            SqlCommand cmd = new SqlCommand("SELECT code,dec FROM reg_education", scon);
            //cmd.Parameters.AddWithValue("@svccode1", svccode);
            SqlDataReader reader = cmd.ExecuteReader();

            GridView1.DataSource = reader;
            GridView1.DataBind();
            scon.Close();

            readtodatatableeducation(GridView1, cid_label);
        }


        public void BindDatahealthinfo(GridView GridView1, Label cid_label)
        {
            string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
            SqlConnection scon = new SqlConnection(constr);


            //string svccode1 = svccode;
            scon.Open();
            SqlCommand cmd = new SqlCommand("SELECT code,dec FROM reg_heal", scon);
            //cmd.Parameters.AddWithValue("@svccode1", svccode);
            SqlDataReader reader = cmd.ExecuteReader();

            GridView1.DataSource = reader;
            GridView1.DataBind();
            scon.Close();

            readtodatatable(GridView1, cid_label);
        }


        public void insertdatatodbheal(GridView GridView1, Label cid_label)
        {
            string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
            SqlConnection scon = new SqlConnection(constr);

            //bool selectorChecked4 = false;

            DataTable dt2heal = new DataTable();
            dt2heal.Columns.Add("SACTCode");

            dt2heal.Columns.Add("note");
            foreach (GridViewRow row in GridView1.Rows)
            {
                CheckBox cb = (CheckBox)row.FindControl("CaseSelector4");
                if (cb != null && cb.Checked)
                {
                    //selectorChecked4 = true;
                    TextBox rb = row.FindControl("TextBox3") as TextBox;
                    DataRow dr2heal;
                    dr2heal = dt2heal.NewRow();
                    string SACTCode = GridView1.Rows[row.RowIndex].Cells[0].Text;
                    // string CID = GridView2.Rows[row.RowIndex].Cells[1].Text;
                    //SelectStat.Text += string.Format("{0} has been added <br />", SACTCode);
                    //  string CID = GridView1.Rows[row.RowIndex].Cells[1].Text;
                    string note = rb.Text;
                    dt2heal.Rows.Add(SACTCode, note);


                }
            }




            SqlCommand command2 = new SqlCommand("delete from [healthinfo] where  CID=@CID", scon);
            scon.Open();
            command2.Parameters.AddWithValue("@CID", cid_label.Text);
            command2.ExecuteNonQuery();
            scon.Close();



            for (int i = 0; i < dt2heal.Rows.Count; i++)
            {


                SqlCommand command = new SqlCommand("INSERT INTO [healthinfo] (code,CID,Note,[update]) VALUES('" + dt2heal.Rows[i]["SACTCode"].ToString() + "',@PSNumber,'" + dt2heal.Rows[i]["note"].ToString() + "',@update)", scon);
                scon.Open();
                command.Parameters.AddWithValue("@PSNumber", cid_label.Text);
                command.Parameters.AddWithValue("@update", DateTime.Now);
                command.ExecuteNonQuery();
                scon.Close();

                SqlCommand commandlog = new SqlCommand("INSERT INTO [healthinfo_log] (code,CID,Note,[update]) VALUES('" + dt2heal.Rows[i]["SACTCode"].ToString() + "',@PSNumber,'" + dt2heal.Rows[i]["note"].ToString() + "',@update)", scon);
                scon.Open();
                commandlog.Parameters.AddWithValue("@PSNumber", cid_label.Text);
                commandlog.Parameters.AddWithValue("@update", DateTime.Now);
                commandlog.ExecuteNonQuery();
                scon.Close();



            }




            BindDatahealthinfo(GridView1, cid_label);

        }









        public void insertdatatodbeducation(GridView GridView1, Label cid_label)
        {
            string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
            SqlConnection scon = new SqlConnection(constr);

            //bool selectorChecked4 = false;

            DataTable dt2edu = new DataTable();
            dt2edu.Columns.Add("SACTCode");

            dt2edu.Columns.Add("note");
            foreach (GridViewRow row in GridView1.Rows)
            {
                CheckBox cb = (CheckBox)row.FindControl("CaseSelectoredu");
                if (cb != null && cb.Checked)
                {
                    //selectorChecked4 = true;
                    TextBox rb = row.FindControl("TextBoxedu") as TextBox;
                    DataRow dr2edu;
                    dr2edu = dt2edu.NewRow();
                    string SACTCode = GridView1.Rows[row.RowIndex].Cells[0].Text;
                    // string CID = GridView2.Rows[row.RowIndex].Cells[1].Text;
                    //SelectStat.Text += string.Format("{0} has been added <br />", SACTCode);
                    //  string CID = GridView1.Rows[row.RowIndex].Cells[1].Text;
                    string note = rb.Text;
                    dt2edu.Rows.Add(SACTCode, note);


                }
            }




            SqlCommand command2 = new SqlCommand("delete from [education] where  CID=@CID", scon);
            scon.Open();
            command2.Parameters.AddWithValue("@CID", cid_label.Text);
            command2.ExecuteNonQuery();
            scon.Close();



            for (int i = 0; i < dt2edu.Rows.Count; i++)
            {


                SqlCommand command = new SqlCommand("INSERT INTO [education] (code,CID,Note,[update]) VALUES('" + dt2edu.Rows[i]["SACTCode"].ToString() + "',@PSNumber,'" + dt2edu.Rows[i]["note"].ToString() + "',@update)", scon);
                scon.Open();
                command.Parameters.AddWithValue("@PSNumber", cid_label.Text);
                command.Parameters.AddWithValue("@update", DateTime.Now);
                command.ExecuteNonQuery();
                scon.Close();

                SqlCommand commandlog = new SqlCommand("INSERT INTO [education_log] (code,CID,Note,[update]) VALUES('" + dt2edu.Rows[i]["SACTCode"].ToString() + "',@PSNumber,'" + dt2edu.Rows[i]["note"].ToString() + "',@update)", scon);
                scon.Open();
                commandlog.Parameters.AddWithValue("@PSNumber", cid_label.Text);
                commandlog.Parameters.AddWithValue("@update", DateTime.Now);
                commandlog.ExecuteNonQuery();
                scon.Close();



            }




            BindDatareg_educationnfo(GridView1, cid_label);

        }












        //ดึงค่าจาก db มาโชว์ในกริดวิว


        public void readtodatatable(GridView GridView1, Label cid_label)
        {
            string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
            SqlConnection scon = new SqlConnection(constr);


            scon.Open();
            DataTable dtheal = new DataTable();

            SqlCommand cmd = new SqlCommand("SELECT code,Note from healthinfo where CID=@CID", scon);
            cmd.Parameters.AddWithValue("@CID", cid_label.Text);

            SqlDataReader readerheal = cmd.ExecuteReader();

            dtheal.Columns.Add("code");
            dtheal.Columns.Add("Note");
            while (readerheal.Read() != false)
            {
                dtheal.Rows.Add(readerheal["code"].ToString(), (readerheal["Note"].ToString()));
                //x3.Add(reader8["ACTCode"].ToString());
                //x4.Add(reader8["Note"].ToString());

            }
            readerheal.Close();


            for (int j = 0; j < dtheal.Rows.Count; j++)
            {
                string a = dtheal.Rows[j]["code"].ToString();
                foreach (GridViewRow GR in GridView1.Rows)
                {
                    string b = GR.Cells[0].Text.ToString();
                    CheckBox chk = (CheckBox)GR.FindControl("CaseSelector4");
                    TextBox tb = (TextBox)GR.FindControl("TextBox3");
                    if (a == b)
                    {

                        tb.Text = dtheal.Rows[j]["Note"].ToString();
                        // tb.Text=
                        chk.Checked = true;
                    }
                }
            }


            scon.Close();




        }


        public void readtodatatableeducation(GridView GridView1, Label cid_label)
        {
            string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
            SqlConnection scon = new SqlConnection(constr);


            scon.Open();
            DataTable dtedu = new DataTable();

            SqlCommand cmd = new SqlCommand("SELECT code,Note from education where CID=@CID", scon);
            cmd.Parameters.AddWithValue("@CID", cid_label.Text);

            SqlDataReader reader8 = cmd.ExecuteReader();

            dtedu.Columns.Add("code");
            dtedu.Columns.Add("Note");
            while (reader8.Read() != false)
            {
                dtedu.Rows.Add(reader8["code"].ToString(), (reader8["Note"].ToString()));
                //x3.Add(reader8["ACTCode"].ToString());
                //x4.Add(reader8["Note"].ToString());

            }
            reader8.Close();


            for (int j = 0; j < dtedu.Rows.Count; j++)
            {
                string a = dtedu.Rows[j]["code"].ToString();
                foreach (GridViewRow GR in GridView1.Rows)
                {
                    string b = GR.Cells[0].Text.ToString();
                    CheckBox chk = (CheckBox)GR.FindControl("CaseSelectoredu");
                    TextBox tb = (TextBox)GR.FindControl("TextBoxedu");
                    if (a == b)
                    {

                        tb.Text = dtedu.Rows[j]["Note"].ToString();
                        // tb.Text=
                        chk.Checked = true;
                    }
                }
            }


            scon.Close();




        }









        public void insertregdataeducationinfo(Label cid_label, String School, String level, String type, String Personnel, String phone)
        {
            string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
            SqlConnection conn2 = new SqlConnection(constr);


            SqlCommand CmdSql = new SqlCommand("UPDATE [educationinfo] SET School=@School,level=@level,type=@type,Personnel=@Personnel,phone=@phone WHERE CID=@CID", conn2);
            conn2.Open();
            CmdSql.Parameters.AddWithValue("@School", School);
            CmdSql.Parameters.AddWithValue("@level", level);
            CmdSql.Parameters.AddWithValue("@type", type);
            CmdSql.Parameters.AddWithValue("@Personnel", Personnel);
            CmdSql.Parameters.AddWithValue("@phone", phone);
            CmdSql.Parameters.AddWithValue("@CID", cid_label.Text);

            CmdSql.ExecuteNonQuery();
            conn2.Close();
            //MessageBox("ลงทะเบียนเรียบร้อยครับ");
            //ImageUpload_f();
            // Response.Redirect("~/regfamily.aspx");
        }









        //โหลด db to textbox
        public void loaddataeducationinfo(Label cid_label, TextBox School, TextBox level, TextBox type, TextBox Personnel, TextBox phone)
        {
            string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
            SqlConnection conn = new SqlConnection(constr);

            SqlCommand CmdSql2 = new SqlCommand("SELECT School,level,type,Personnel,phone,CID FROM [educationinfo] where CID=@CID", conn);
            conn.Open();
            CmdSql2.Parameters.AddWithValue("@CID", cid_label.Text);

            SqlDataReader reader2 = CmdSql2.ExecuteReader();



            if (reader2.Read())
            {
                School.Text = reader2["School"].ToString();
                level.Text = reader2["level"].ToString();
                type.Text = reader2["type"].ToString();
                Personnel.Text = reader2["Personnel"].ToString();
                phone.Text = reader2["phone"].ToString();



            }
            else
            {

            }
            conn.Close();
            reader2.Close();

        }







        public void radiomedicalcare(RadioButtonList medicalcare)
        {
            string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
            SqlConnection conn = new SqlConnection(constr);
            SqlCommand CmdSql22 = new SqlCommand("SELECT id,dec FROM [r_medicalcare]", conn);
            conn.Open();

            DataTable dt2 = new DataTable();

            SqlDataAdapter sqlDa2 = new SqlDataAdapter();

            sqlDa2.SelectCommand = CmdSql22;
            sqlDa2.Fill(dt2);
            if (dt2.Rows.Count > 0)
            {
                medicalcare.DataSource = dt2;
                medicalcare.DataTextField = "dec"; //Where Anwsers is the column name from the database
                medicalcare.DataValueField = "id";
                medicalcare.DataBind();
            }
            conn.Close();



        }







        public void radioyesno_1(RadioButtonList yesno1)
        {
            string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
            SqlConnection conn = new SqlConnection(constr);
            SqlCommand CmdSql22 = new SqlCommand("SELECT id,dec FROM [r_yesno]", conn);
            conn.Open();

            DataTable dt2 = new DataTable();

            SqlDataAdapter sqlDa2 = new SqlDataAdapter();

            sqlDa2.SelectCommand = CmdSql22;
            sqlDa2.Fill(dt2);
            if (dt2.Rows.Count > 0)
            {
                yesno1.DataSource = dt2;
                yesno1.DataTextField = "dec"; //Where Anwsers is the column name from the database
                yesno1.DataValueField = "id";
                yesno1.DataBind();
            }
            conn.Close();



        }




        public void radioyesno_2(RadioButtonList yesno2)
        {
            string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
            SqlConnection conn = new SqlConnection(constr);
            SqlCommand CmdSql22 = new SqlCommand("SELECT id,dec FROM [r_yesno]", conn);
            conn.Open();

            DataTable dt2 = new DataTable();

            SqlDataAdapter sqlDa2 = new SqlDataAdapter();

            sqlDa2.SelectCommand = CmdSql22;
            sqlDa2.Fill(dt2);
            if (dt2.Rows.Count > 0)
            {
                yesno2.DataSource = dt2;
                yesno2.DataTextField = "dec"; //Where Anwsers is the column name from the database
                yesno2.DataValueField = "id";
                yesno2.DataBind();
            }
            conn.Close();



        }










        public void insertregdatarightsinfo(Label cid_label, String MedicalIDcard, String Dateofissue, String Dateofexp, String PrimaryCareClinic, String CareClinicForward, String NumberDisabilitieID, String NumberDisabilitieIDdate, String NumberDisabilitieIDexp, RadioButtonList medicalcare, RadioButtonList yesno1, RadioButtonList yesno2, DropDownList DisabilityType)
        {
            string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
            SqlConnection conn2 = new SqlConnection(constr);


            SqlCommand CmdSql = new SqlCommand("UPDATE [rightsinfo] SET MedicalIDcard=@MedicalIDcard,Dateofissue=@Dateofissue,Dateofexp=@Dateofexp,PrimaryCareClinic=@PrimaryCareClinic,CareClinicForward=@CareClinicForward,NumberDisabilitieID=@NumberDisabilitieID,NumberDisabilitieIDdate=@NumberDisabilitieIDdate,NumberDisabilitieIDexp=@NumberDisabilitieIDexp,medicalcare=@medicalcare,DisabilityAllowance=@DisabilityAllowance,CouponStudy=@CouponStudy,DisabilityType=@DisabilityType WHERE CID=@CID", conn2);
            conn2.Open();

            CmdSql.Parameters.AddWithValue("@CID", cid_label.Text);

            CmdSql.Parameters.AddWithValue("@MedicalIDcard", MedicalIDcard);
            CmdSql.Parameters.AddWithValue("@Dateofissue", Dateofissue);
            CmdSql.Parameters.AddWithValue("@Dateofexp", Dateofexp);
            CmdSql.Parameters.AddWithValue("@PrimaryCareClinic", PrimaryCareClinic);
            CmdSql.Parameters.AddWithValue("@CareClinicForward", CareClinicForward);
            CmdSql.Parameters.AddWithValue("@NumberDisabilitieID", NumberDisabilitieID);
            CmdSql.Parameters.AddWithValue("@NumberDisabilitieIDdate", NumberDisabilitieIDdate);
            CmdSql.Parameters.AddWithValue("@NumberDisabilitieIDexp", NumberDisabilitieIDexp);
            CmdSql.Parameters.AddWithValue("@medicalcare", medicalcare.SelectedValue);
            CmdSql.Parameters.AddWithValue("@DisabilityAllowance", yesno1.SelectedValue);
            CmdSql.Parameters.AddWithValue("@CouponStudy", yesno2.SelectedValue);



            CmdSql.Parameters.AddWithValue("@DisabilityType", DisabilityType.SelectedValue);


            CmdSql.ExecuteNonQuery();
            conn2.Close();

        }









        //โหลด db to textbox rightsinfo
        public void loaddatarightsinfo(Label cid_label, TextBox MedicalIDcard, TextBox Dateofissue, TextBox Dateofexp, TextBox PrimaryCareClinic, TextBox CareClinicForward, TextBox NumberDisabilitieID, TextBox NumberDisabilitieIDdate, TextBox NumberDisabilitieIDexp)
        {
            string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
            SqlConnection conn = new SqlConnection(constr);

            SqlCommand CmdSql2 = new SqlCommand("SELECT [CID],[MedicalIDcard],[Dateofissue],[Dateofexp],[PrimaryCareClinic],[CareClinicForward],[NumberDisabilitieID],[NumberDisabilitieIDdate],[NumberDisabilitieIDexp] FROM [rightsinfo] where CID=@CID", conn);
            conn.Open();
            CmdSql2.Parameters.AddWithValue("@CID", cid_label.Text);

            SqlDataReader reader2 = CmdSql2.ExecuteReader();






            if (reader2.Read())
            {



                MedicalIDcard.Text = reader2["MedicalIDcard"].ToString();
                Dateofissue.Text = reader2["Dateofissue"].ToString();
                Dateofexp.Text = reader2["Dateofexp"].ToString();
                PrimaryCareClinic.Text = reader2["PrimaryCareClinic"].ToString();
                CareClinicForward.Text = reader2["CareClinicForward"].ToString();
                NumberDisabilitieID.Text = reader2["NumberDisabilitieID"].ToString();

                NumberDisabilitieIDdate.Text = reader2["NumberDisabilitieIDdate"].ToString();
                NumberDisabilitieIDexp.Text = reader2["NumberDisabilitieIDexp"].ToString();

            }
            else
            {

            }
            conn.Close();
            reader2.Close();

        }














        //ดึงข้อมูลสิทธิมาโชว์

        public void loaddataradiomedicalcare(Label cid_label, RadioButtonList medicalcare)
        {
            string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
            SqlConnection conn = new SqlConnection(constr);

            SqlCommand CmdSql2 = new SqlCommand("SELECT b.id AS id FROM [rightsinfo] a join [r_medicalcare] b on a.medicalcare=b.id where a.CID=@CID", conn);
            conn.Open();
            CmdSql2.Parameters.AddWithValue("@CID", cid_label.Text);

            SqlDataReader reader2 = CmdSql2.ExecuteReader();



            if (reader2.Read())
            {

                medicalcare.Text = reader2["id"].ToString();

            }
            else
            {

            }
            conn.Close();
            reader2.Close();

        }







        //ดึงข้อมูลเบี้ยยังชีพมาโชว์


        public void loaddataradioDisabilityAllowance(Label cid_label, RadioButtonList yesno1)
        {
            string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
            SqlConnection conn = new SqlConnection(constr);

            SqlCommand CmdSql2 = new SqlCommand("SELECT b.id AS id FROM [rightsinfo] a join [r_yesno] b on a.DisabilityAllowance=b.id where a.CID=@CID", conn);
            conn.Open();
            CmdSql2.Parameters.AddWithValue("@CID", cid_label.Text);

            SqlDataReader reader2 = CmdSql2.ExecuteReader();



            if (reader2.Read())
            {

                yesno1.Text = reader2["id"].ToString();

            }
            else
            {

            }
            conn.Close();
            reader2.Close();

        }




        //โหลดข้อมูลคูปองการศึกษามาโชว์

        public void loaddataradioCouponStudy(Label cid_label, RadioButtonList yesno2)
        {
            string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
            SqlConnection conn = new SqlConnection(constr);

            SqlCommand CmdSql2 = new SqlCommand("SELECT b.id AS id FROM [rightsinfo] a join [r_yesno] b on a.CouponStudy=b.id where a.CID=@CID", conn);
            conn.Open();
            CmdSql2.Parameters.AddWithValue("@CID", cid_label.Text);

            SqlDataReader reader2 = CmdSql2.ExecuteReader();



            if (reader2.Read())
            {

                yesno2.Text = reader2["id"].ToString();

            }
            else
            {

            }
            conn.Close();
            reader2.Close();

        }




        public void loaddatadropdownDisabilityType(DropDownList DisabilityType)
        {
            string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
            SqlConnection conn = new SqlConnection(constr);

            SqlCommand CmdSql2 = new SqlCommand("SELECT DisabilityCode,Name FROM DisabilityType", conn);
            conn.Open();

            SqlDataReader reader4 = CmdSql2.ExecuteReader();

            DataTable dt = new DataTable();
            DisabilityType.Items.Clear();
            ListItem newItem = new ListItem();
            newItem.Text = "ประเภทความพิการ";
            newItem.Value = "0";
            DisabilityType.Items.Add(newItem);

            while (reader4.Read())
            {
                newItem = new ListItem();
                newItem.Text = reader4["Name"].ToString();
                newItem.Value = reader4["DisabilityCode"].ToString();
                DisabilityType.Items.Add(newItem);
            }

        }






        public void loaddataDisabilityType(Label cid_label, DropDownList DisabilityType)
        {
            string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
            SqlConnection conn = new SqlConnection(constr);

            SqlCommand CmdSql2 = new SqlCommand("SELECT b.DisabilityCode AS id FROM [rightsinfo] a join [DisabilityType] b on a.DisabilityType=b.DisabilityCode where a.CID=@CID", conn);
            conn.Open();
            CmdSql2.Parameters.AddWithValue("@CID", cid_label.Text);

            SqlDataReader reader2 = CmdSql2.ExecuteReader();



            if (reader2.Read())
            {

                DisabilityType.Text = reader2["id"].ToString();

            }
            else
            {

            }
            conn.Close();
            reader2.Close();

        }














    }
}